<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="120px" label-position="left">
			<el-form-item label="档案架编号" prop="codeNo">
				<el-input v-model="form.codeNo" placeholder="档案架编号" maxlength="40" clearable></el-input>
			</el-form-item>
			<el-form-item label="排序号" prop="sortNo">
				<el-input-number v-model="form.sortNo" controls-position="right" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="当前状态" prop="status">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="管理员" prop="manager">
				<select-user-dialog v-model:accountId="form.manager" :mode="true"/>
			</el-form-item>
			<el-form-item label="档案库房" prop="roomId">
				<el-select v-model="form.roomId" clearable filterable remote :remote-method="remoteMethod" style="width: 100%" placeholder="输入库房编号或地址查询">
					<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="备注">
				<el-input type="textarea" :rows="2" maxlength="280" placeholder="备注" v-model="form.remark"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog.vue";

export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			titleMap: {
				add: '创建档案架',
				edit: '编辑档案架',
			},
			form: {
				frameId:'',
				sortNo: 0,
				codeNo: '',
				roomId:"",
				status:'',
				remark:''
			},
			options: [],
			rules: {
				codeNo: [
					{required: true, message: '档案架编号不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.remoteMethod("");
	},
	methods: {
		async remoteMethod(query){
			let res = await this.$API.archives.archivesRoom.getArchivesRoomListForSelect.get({keyword:query});
			if (res.code == 200) {
				this.options = res.data
			}
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.archives.archivesFrame.insertArchivesFrame.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.archives.archivesFrame.updateArchivesFrame.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.frameId = data.frameId
			this.form.sortNo = data.sortNo
			this.form.codeNo = data.codeNo
			this.form.roomId = data.roomId
			this.form.status = data.status
			this.form.remark = data.remark
		},
	}
}
</script>

